package ru.arriah.common.bean;

import java.io.Serializable;
import java.sql.ResultSet;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Locale;

import ru.arriah.common.ejb.help.PrimaryKey;

public abstract class GenericBean implements Serializable {
   protected final static Date DATE_MINIMUM = 
      (new GregorianCalendar(1000, 0, 1, 0, 0)).getTime();

   protected final static Date DATE_MAXIMUM = 
      (new GregorianCalendar(9999, 11, 31, 23, 55)).getTime();

   protected HashMap<String, String> errors;
   protected PrimaryKey pk;
   protected int id;
   protected String selected;

   public abstract void fillFromResultSet(
      ResultSet resultSet
   );
   
   public void fillFromResultSet(
      ResultSet resultSet, String table
   ) {
      //nop
   }

   public String getSelected() {
      return this.selected;
   }

   public void setSelected() {
      this.selected = "selected";
   }

   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public GenericBean() {
      super();
      errors = new HashMap<String, String>();
      pk = null;
      selected = "";
   }

   public void setPk(PrimaryKey pk) {
      this.pk = pk;
   }

   public void setPkFromString(String pk, Locale locale) {
      try {
         this.pk = new PrimaryKey(pk);
      } catch (Exception e) {
         throw new RuntimeException("Can't parse pk given as a string");
      }
   }

   public PrimaryKey getPk() {
      return pk;
   }

   public String getErrorMessage(String propertyName) {
      String errorMessage = errors.get(propertyName);
      return errorMessage == null ? "" : errorMessage;
   }

   public boolean isEmpty(String string) {
      return string == null || string.trim().length() == 0;
   }

   public abstract boolean isValid();
}
